Trò chơi Tic-Tac-Toe, game đánh caro full source code
53.489 lượt xem;
1 using System;
2 using UnityEngine;
3 using UnityEngine.EventSystems;
4
5 namespace Assets.Scripts
6 {
7 public class Cell : CoreBehaviour, IPointerClickHandler
8 {
9 public GameObject spriteCross;
10 public GameObject spriteNought;
11
12 private event Action<Cell> onCellClick;
13
14 public int Row { get; private set; }
15
16 public int Col { get; private set; }
17
18 public Seed Content { get; private set; }
19
20 public bool IsEmpty { get { return Content == Seed.Empty; } }
21
22 public void Init(int row, int col, Action<Cell> onCellClickAction)
23 {
24 Row = row;
25 Col = col;
26
27 name = string.Format("Cell [{0}, {1}]", row, col);
28
29 onCellClick = onCellClickAction;
30
31 Clear();
32 }
33
34 public void Set(Seed seed)
35 {
36 Content = seed;
37
38 switch (seed)
39 {
40 case Seed.Empty:
41 spriteCross.SetActive(false);
42 spriteNought.SetActive(false);
43 break;
44 case Seed.Cross:
45 spriteCross.SetActive(true);
46 spriteNought.SetActive(false);
47 break;
48 case Seed.Nought:
49 spriteCross.SetActive(false);
50 spriteNought.SetActive(true);
51 break;
52 }
53 }
54
55 public void Clear()
56 {
57 Set(Seed.Empty);
58 }
59
60 public bool HasSeed(Seed seed)
61 {
62 return Content == seed;
63 }
64
65 public void OnPointerClick(PointerEventData eventData)
66 {
67 if (onCellClick != null)
68 {
69 onCellClick(this);
70 }
71 }
72 }
73 }
2 using UnityEngine;
3 using UnityEngine.EventSystems;
4
5 namespace Assets.Scripts
6 {
7 public class Cell : CoreBehaviour, IPointerClickHandler
8 {
9 public GameObject spriteCross;
10 public GameObject spriteNought;
11
12 private event Action<Cell> onCellClick;
13
14 public int Row { get; private set; }
15
16 public int Col { get; private set; }
17
18 public Seed Content { get; private set; }
19
20 public bool IsEmpty { get { return Content == Seed.Empty; } }
21
22 public void Init(int row, int col, Action<Cell> onCellClickAction)
23 {
24 Row = row;
25 Col = col;
26
27 name = string.Format("Cell [{0}, {1}]", row, col);
28
29 onCellClick = onCellClickAction;
30
31 Clear();
32 }
33
34 public void Set(Seed seed)
35 {
36 Content = seed;
37
38 switch (seed)
39 {
40 case Seed.Empty:
41 spriteCross.SetActive(false);
42 spriteNought.SetActive(false);
43 break;
44 case Seed.Cross:
45 spriteCross.SetActive(true);
46 spriteNought.SetActive(false);
47 break;
48 case Seed.Nought:
49 spriteCross.SetActive(false);
50 spriteNought.SetActive(true);
51 break;
52 }
53 }
54
55 public void Clear()
56 {
57 Set(Seed.Empty);
58 }
59
60 public bool HasSeed(Seed seed)
61 {
62 return Content == seed;
63 }
64
65 public void OnPointerClick(PointerEventData eventData)
66 {
67 if (onCellClick != null)
68 {
69 onCellClick(this);
70 }
71 }
72 }
73 }